﻿using System;
using System.Windows;
using System.Windows.Controls;
using Acme.Business;
using Acme.Entities;

namespace Acme.Basic
{

    public partial class MainWindow : Window
    {

        const String STRING_EXCEPTIONDIALOGCAPTION = "Unexpected Error";
        const String STRING_CONFIRMDELETECAPTION = "Confirm Delete";
        const String STRING_CONFIRMDELETEMESSAGE = "Are you sure you want to DELETE this event?";

        EventRepository _repository;

        public MainWindow()
        {
            InitializeComponent();
        }

        void Window_Loaded(Object sender, RoutedEventArgs e)
        {
            try
            {
                _repository = new EventRepository();
                LoadSearchResults();
                SetButtons();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, STRING_EXCEPTIONDIALOGCAPTION, MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        void btnSave_Click(Object sender, RoutedEventArgs e)
        {
            try
            {
                var btn = (Button)sender;
                this._repository.Save((Event)btn.DataContext);
                this.DetailsForm.DataContext = null;
                LoadSearchResults();
                SetButtons();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, STRING_EXCEPTIONDIALOGCAPTION, MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        void btnDelete_Click(Object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show(
                STRING_CONFIRMDELETEMESSAGE, STRING_CONFIRMDELETECAPTION,
                MessageBoxButton.OKCancel, MessageBoxImage.Question) != MessageBoxResult.OK) return;
            try
            {
                var btn = (Button)sender;
                this._repository.Remove((Event)btn.DataContext);
                this.DetailsForm.DataContext = null;
                LoadSearchResults();
                SetButtons();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, STRING_EXCEPTIONDIALOGCAPTION, MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        void btnThrow_Click(Object sender, RoutedEventArgs e)
        {
            try
            {
                this._repository.Save(null);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, STRING_EXCEPTIONDIALOGCAPTION, MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        void btnNew_Click(Object sender, RoutedEventArgs e)
        {
            try
            {
                this.lbEvents.SelectedIndex = -1;
                this.DetailsForm.DataContext = this._repository.Create();
                SetButtons();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, STRING_EXCEPTIONDIALOGCAPTION, MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        void lbEvents_SelectionChanged(Object sender, SelectionChangedEventArgs e)
        {
            if (this.lbEvents.SelectedIndex != -1 && this.lbEvents.SelectedItem != null)
            {
                try
                {
                    var eventSearchResult = (EventSearchResult)this.lbEvents.SelectedItem;
                    this.DetailsForm.DataContext = _repository.GetById(eventSearchResult.Id);
                    SetButtons();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, STRING_EXCEPTIONDIALOGCAPTION, MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
        }

        void btnCancel_Click(Object sender, RoutedEventArgs e)
        {
            try
            {
                this.DetailsForm.DataContext = null;
                this.lbEvents.SelectedIndex = -1;
                SetButtons();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, STRING_EXCEPTIONDIALOGCAPTION, MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        void LoadSearchResults()
        {
            try
            {
                this.lbEvents.ItemsSource = _repository.GetSearchResults();
                this.lbEvents.SelectedIndex = -1;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, STRING_EXCEPTIONDIALOGCAPTION, MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        void SetButtons()
        {

            if (this.DetailsForm.DataContext == null)
            {
                this.btnCancel.IsEnabled = false;
                this.btnSave.IsEnabled = false;
                this.btnDelete.IsEnabled = false;
                this.btnThrow.IsEnabled = false;
            }
            else
            {
                this.btnCancel.IsEnabled = true;
                this.btnSave.IsEnabled = true;
                this.btnDelete.IsEnabled = true;
                this.btnThrow.IsEnabled = true;
            }
        }
    }
}
